package br.com.timecardforpeon.dialog;

import static br.com.timecardforpeon.util.Util.DATE_FORMAT_FOR_VIEW;
import static java.util.Calendar.DATE;
import static java.util.Calendar.MONTH;
import static java.util.Calendar.YEAR;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import br.com.timecardforpeon.R;
import br.com.timecardforpeon.csv.ExportCsvTask;
import br.com.timecardforpeon.util.Util;

public class ExportDialog extends Dialog {

	private TextView fromDate;
	private TextView toDate;
	private Button exportButton;
	private Button cancelButton;
	
	public ExportDialog(Context context) {
		super(context);
		
		this.setContentView(R.layout.export_dialog);
		this.setTitle(R.string.export);
		
		this.initializeComponents();
		this.initializeListeners();
	}

	private void initializeComponents() {
		
		Calendar date = Calendar.getInstance();
		date.set(Calendar.DATE, 1);
		date.set(Calendar.MONTH, date.get(Calendar.MONTH) - 1);
		
		this.fromDate = Util.<TextView>getViewById(this, R.id.fromDateText);
		this.fromDate.setText(Util.dateToString(DATE_FORMAT_FOR_VIEW, date.getTime()));
		
		date.set(Calendar.DATE, date.getActualMaximum(Calendar.DATE));
		this.toDate = Util.<TextView>getViewById(this, R.id.toDateText);
		this.toDate.setText(Util.dateToString(DATE_FORMAT_FOR_VIEW, date.getTime()));
		
		this.exportButton = Util.<Button>getViewById(this, R.id.exportButton);
		this.cancelButton = Util.<Button>getViewById(this, R.id.cancelButton);
	}

	private void initializeListeners() {
		
		this.fromDate.setOnClickListener(new DateClickListener(this, this.fromDate));
		this.toDate.setOnClickListener(new DateClickListener(this, this.toDate));
		
		this.fromDate.addTextChangedListener(new FromDateTextChangedListener());

		this.exportButton.setOnClickListener(new ExportClickListener(this));
		this.cancelButton.setOnClickListener(new CancelClickListener(this));
	}

	private final class DateClickListener implements android.view.View.OnClickListener, OnDateSetListener {
		
		private Dialog dialog;
		private TextView textView;

		public DateClickListener(Dialog dialog, TextView textView) {
			super();
			this.dialog = dialog;
			this.textView = textView;
		}
		
		@Override
		public void onClick(View v) {
			
			try {
				
				Calendar date = Calendar.getInstance();
				date.setTime(Util.stringToDate(DATE_FORMAT_FOR_VIEW, this.textView.getText().toString()));

				DatePickerDialog datePickerDialog = new DatePickerDialog(
						this.dialog.getContext(),
						this,
						date.get(YEAR),
						date.get(MONTH),
						date.get(DATE));
				
				datePickerDialog.show();
				
			} catch (ParseException e) {
				Log.e("timeCard4Peon", e.getMessage(), e);
			}
		}
		
		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
			
			Calendar date = Calendar.getInstance();
			date.set(YEAR, year);
			date.set(MONTH, monthOfYear);
			date.set(DATE, dayOfMonth);
			
			this.textView.setText(Util.dateToString(DATE_FORMAT_FOR_VIEW, date.getTime()));
		}
	}
	
	private final class FromDateTextChangedListener implements TextWatcher {
		
		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {

			try {
				
				Date fromDate = Util.stringToDate(DATE_FORMAT_FOR_VIEW, s.toString());
				
				Calendar toDate = Calendar.getInstance();
				toDate.setTime(fromDate);
				toDate.set(Calendar.DAY_OF_MONTH, toDate.getActualMaximum(Calendar.DAY_OF_MONTH));
				
				ExportDialog.this.toDate.setText(Util.dateToString(DATE_FORMAT_FOR_VIEW, toDate.getTime()));
				
			} catch (ParseException e) {
				Log.e("timeCard4Peon", e.getMessage(), e);
			}
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
		}

		@Override
		public void afterTextChanged(Editable s) {
		}
	}

	private final class ExportClickListener implements android.view.View.OnClickListener {

		private Dialog dialog;
		
		public ExportClickListener(Dialog dialog) {
			this.dialog = dialog;
		}

		@Override
		public void onClick(View v) {
			
			ExportCsvTask task = new ExportCsvTask(getContext(), fromDate.getText(), toDate.getText());
			task.execute();
			
			this.dialog.dismiss();
		}
	}
	
	private final class CancelClickListener implements android.view.View.OnClickListener {
		
		private Dialog dialog;
		
		public CancelClickListener(Dialog dialog) {
			this.dialog = dialog;
		}

		@Override
		public void onClick(View view) {
			this.dialog.dismiss();
		}
	}
}